<?php
include_once("include/connect.php");
//paging setup
if(!isset($_GET['page']))
{
	$getpage=1;
}
else $getpage=$_GET['page'];
$limit = 40;
$start = $getpage*$limit-$limit;
// end of paging
$load = mysql_query("select * from loai");
if(isset($_POST['search_form_submit']))
{
$keyword="";
$series="";
$category="";
$country="";
$translation="";
$quality="";
$sort="";

$sql="select * from phim,cttheloai where cttheloai.maphim=phim.maphim and linkanh is not null ";

if($_POST['keyword']!="")
	$keyword=" and tenphim like '%".$_POST['keyword']."%'";
	
if($_POST['series']!=0)
	$series=" and maloai='".$_POST['series']."'";
	
if($_POST['category']!=0)
	$category=" and matheloai='".$_POST['category']."' and cttheloai.maphim=phim.maphim";
if($_POST['country']!=0)
	$country=" and maquocgia='".$_POST['country']."'";
if($_POST['translation']!=0)
	$translation=" and machuyenngu='".$_POST['translation']."'";
if($_POST['quality']!=0)
	$quality=" and machatluong='".$_POST['quality']."'";

if($_POST['sort']==1)
	$sort=" group by phim.maphim order by ngaydang asc";
else
	$sort=" group by phim.maphim order by view desc";

$sql_pri="$sql $keyword $series $category $country $translation $quality $sort";
$_GET['key']='search';
$_GET['sql']=$sql_pri;
}
if(isset($_GET['key']))
{
		switch($_GET['key']){
		case "year":
			$query_all="select * from phim where ngaydang like '%{$_GET['id']}%' and linkanh is NOT NULL order by maphim";
			$link="index.php?key=year&id={$_GET['id']}&page=";
			$query="select * from phim where ngaydang like '%{$_GET['id']}%' order by maphim DESC limit {$start},{$limit}";
			$ps_title="Phim theo năm {$_GET['id']}";
			break;
		case "act":
			$query_all="select * from phim,ctdienvien where phim.maphim = ctdienvien.maphim and ctdienvien.madienvien = {$_GET['id']} and linkanh is NOT NULL";
			$link="index.php?key=act&id={$_GET['id']}&page=";
			$query=$query_all." limit {$start},{$limit} ";
			$ps_title="Phim cùng diễn viên";
			break;
		case "dir":
			$query_all="select * from phim where madaodien={$_GET['id']} order by maphim and linkanh is NOT NULL";
			$link="index.php?key=dir&id={$_GET['id']}&page=";	
			$query="select * from phim where madaodien={$_GET['id']} order by maphim DESC limit {$start},{$limit}";
			$ps_title="Phim cùng đạo diễn";
			break;
		case "prod":
			$query_all="select * from phim where manhasx={$_GET['id']} and linkanh is NOT NULL";
			$link="index.php?key=prod&id={$_GET['id']}&page=";
			$query="select * from phim where manhasx={$_GET['id']} order by maphim  DESC limit {$start},{$limit}";
			$ps_title="Phim cùng nhà sản xuất";
			break;
		case "new":
			$query_all="select * from phim where linkanh is NOT NULL order by maphim DESC ";
			$link="index.php?key=new&page=";
			$query=$query_all." limit {$start},{$limit}";
			$ps_title="Phim mới";
			break;	
		case "request":
			$query_all="select * from phim where linkanh is NOT NULL order by view DESC";
			$link="index.php?key=new&page=";
			$query=$query_all." limit {$start},{$limit}";
			$ps_title="Phim đựoc yêu cầu nhiều";
			break;	
		case "mostviewed":
			$query_all="select * from phim where linkanh is NOT NULL order by view,maphim DESC ";
			$link="index.php?key=mostviewed&page=";
			$query=$query_all."limit {$start},{$limit}";	
			$ps_title="Phim xem nhiều nhất";
			break;
		
		case "detail":
			$select =mysql_query("select * from cttheloai where maphim ={$_GET['id']}");
			$theloai=mysql_fetch_array($select);
			$query_all="select * from phim,cttheloai where cttheloai.maphim <> {$_GET['id']} and matheloai={$theloai['matheloai']} and phim.linkanh is not null group by phim.maphim ";
			$link="index.php?key=detail&id={$_GET['id']}&page=";$limit=12;$start = $getpage*$limit-$limit;
			$query=$query_all." limit {$start},{$limit}";
			$ps_title="Phim cùng chủ đề";
			break;
		case "loai":
			$query_all="select * from phim where maloai={$_GET['id']}";
			$link="index.php?key=loai&id={$_GET['id']}&page=";
			$query=$query_all." order by maphim DESC limit {$start},{$limit}";
			$ps_title=$_GET['name'];
			break;
		case "theloai":
			$query_all="select * from phim,cttheloai where phim.maphim = cttheloai.maphim and cttheloai.matheloai = {$_GET['id']} and phim.linkanh is not null";
			$query=$query_all." order by phim.maphim DESC limit {$start},{$limit}";
			$ps_title=$_GET['name'];
			break;
		case "quocgia":
			$query_all="select * from phim,quocgia where phim.maquocgia = quocgia.maquocgia and quocgia.maquocgia = {$_GET['id']} ";
			$link="index.php?key=quocgia&id={$_GET['id']}&page=";
			$query=$query_all." order by phim.maphim DESC limit {$start},{$limit}";
			$ps_title=$_GET['name'];
			break;
		case "chuyenngu":
			$query_all="select * from phim,chuyenngu where phim.machuyenngu = chuyenngu.machuyenngu and chuyenngu.machuyenngu = {$_GET['id']} ";
			$link="index.php?key=chuyenngu&id={$_GET['id']}&page=";
			$query=$query_all." order by chuyenngu.machuyenngu DESC limit {$start},{$limit}";
			$ps_title=$_GET['name'];
			break;
		case "chatluong":
			$query_all="select * from phim,chatluong where phim.machatluong = chatluong.machatluong and chatluong.machatluong = {$_GET['id']} ";
			$link="index.php?key=chatluong&id={$_GET['id']}&page=";
			$query=$query_all." order by chatluong.machatluong DESC limit {$start},{$limit}";
			$ps_title=$_GET['name'];
			break;
		case "search":
			$query_all=$query=$_GET['sql'];
			$ps_title="Tìm kiếm";
			break;
		default:
			$query_all="select * from phim where linkanh is NOT NULL";
			$link="index.php?key=default&id={$_GET['id']}&page=";
			$query=$query_all." order by maphim DESC limit {$start},{$limit}";
			$ps_title="Trang chủ";
			break;
	};
}
else {
	$query_all="select * from phim where linkanh is NOT NULL";
	$link="index.php?key=default";
	$query=$query_all." order by maphim DESC limit {$start},{$limit}";
	$ps_title="Trang chủ";
}
?>
<h1 class="ps_title"><?php echo $ps_title ?></h1>

<div class="clear"></div>
<!--list-->
<?php 
$s=mysql_query($query) or die(mysql_error());
while($d=mysql_fetch_array($s))
{
?>
<div class="film_short">
	<div class="status">
		<span class="status_r">
			<span class="process">
				<span>
				<?php 
					$chatluong=mysql_query("select * from chatluong where machatluong={$d['machatluong']}");
					$cl=mysql_fetch_array($chatluong);
					echo $cl['tenchatluong'];
				?>
				</span>
			</span>
		</span>
	</div>
	<div class="poster">
		<a class="thumbnail tooltip" href="<?php echo "index.php?key=detail&id={$d['maphim']}&maloai={$d['maloai']}";?>">
		<img src="<?php echo $d['linkthumbnail'] ?>" alt="<?php echo $d['tenphim'] ?>" original="<?php echo $d['linkthumbnail'] ?>" style="display: inline;width:125px;height:160px">
		</a>
		<div class="tooltip_content"><span class="black"><?php echo $d['tenphim'] ?></span>
			<div class="some_info">
			<?php 
			$theloai=mysql_query("select tentheloai from cttheloai,theloai,phim where phim.maphim = cttheloai.maphim and cttheloai.matheloai=theloai.matheloai and phim.maphim = {$d['maphim']}");
			$phude=mysql_query("select * from chuyenngu,phim where phim.machuyenngu=chuyenngu.machuyenngu and maphim={$d['maphim']}");
			?>
			Thể loại: <b><?php while($tl=mysql_fetch_array($theloai)) {echo $tl['tentheloai'];} ?></b><br>
			Chuyển ngữ: <b><?php $pd=mysql_fetch_array($phude); echo $pd['tenchuyenngu']?></b><br>
			Chất lượng: <b><?php echo $cl['tenchatluong']?></b><br>
			</div>
			<?php echo preg_replace("/<img[^>]+\>/i", "(image) ", $d['gioithieu'])?>
		</div>
		<div class="star">
        	<?php
			$rating = mysql_query("select AVG(diem) from rate,thongtinphim where rate.marate=thongtinphim.marate and thongtinphim.maphim = {$d['maphim']}") or die(mysql_error());
			$r=mysql_fetch_array($rating);
			$diem=$r['AVG(diem)'];
			for($i=1;$i<=5;$i++)
			{
				if($i<$diem)
				{
					echo "<span class='star_on'></span>";
				}
				else
				{ 
					if(($i-0.5)<$diem)
						echo "<span class='star_half'></span>";
					else 
						echo "<span class='star_off'></span>";
				}
			}
			?>
		</div>
	</div>
<a class="name" href="<?php echo "include/watch.php?id={$d['maphim']}";?>"><?php echo $d['tenphim'] ?></a>
</div>
<!--end of list-->
<?php }?>
<!--page-->
<?php 
$for=0;
?>
<div class="clear"></div>
<div class="ps_paging">
	<?php 
	$page_limit=ceil(mysql_num_rows(mysql_query($query_all))/$limit)+1;
	if($page_limit!=2){
		if($getpage>2)	
			$start_page=$getpage-2;
		else $start_page=1;
		if($getpage!=1) {
		?>
		<a href="<?php echo $link.($page-1)?>">« Trở về</a>
		<?php }
		for($i=$start_page;$i<$page_limit;$i++)
		{
			?>
			<a <?php if($i==$getpage) echo "class='paging_active'" ?> href="<?php echo $link.$i?>"><?php echo $i?></a>  
			<?php 
		}
		if($getpage<$page_limit-1) {
		?>
		<a href="<?php echo $link.($page+1)?>">Tiếp theo »</a>
    <?php }
	}?>
</div>

<div class="clear"></div>

<!--keys-->
<div class="tag ps_padding_10">
   <b>Từ khóa tìm kiếm:</b> 
   <a class="remove_title" href="<?php echo $link ?>"><?php echo $ps_title ?></a>
</div>

